//
//  AppDelegate.swift
//  Weibo
//
//  Created by lzing on 16/3/19.
//  Copyright © 2016年 lzing. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        setupAppearance()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.rootViewController = defaultViewController()
        
        window?.rootViewController?.view.backgroundColor = UIColor.whiteColor()
        
        window?.makeKeyAndVisible()
        
        return true
    }
    
    //MARK: - judgeWhichIsTheBestController
    private func defaultViewController() -> UIViewController {
        // 是否登录,如果没登录就是main
        if LZUserAccountViewModel.sharedUserAccountViewModel.userLoginStatus == false {
            return LZMainController()
        }
        // 如果是新版本就newFeature,不是新版本就是welcome
        return isNewFeature() ? LZNewFeatureController() : LZWelcomeController()
    }
    
    //MARK: - switchController
    private func switchRootViewController(controller : UIViewController) {
        window?.rootViewController = controller
    }
    
    ///  class switchController
    class func outSwitchRootViewController(controller : UIViewController) {
        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
        appDelegate?.switchRootViewController(controller)
    }
    
    //MARK: - isNewFeature
    private func isNewFeature() -> Bool {
        // 取出当前版本号
        let nowVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        // 取出沙盒版本号
        let sandBoxKey = "sandBoxKey"
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandBoxKey)
        // 比较,不一样就是新版本
        let result = nowVersion != sandBoxVersion
        // 如果是新版本,记录版本号
        if result {
            NSUserDefaults.standardUserDefaults().setObject(nowVersion, forKey: sandBoxKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        // 返回比较结果
        return result
    }
    
    //MARK: - setupAppearance
    @objc private func setupAppearance() {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }

}

